package com.woniuxy.java106crm.controller;

import com.woniuxy.java106crm.annotation.RequireMethod;
import com.woniuxy.java106crm.entity.Plan;
import com.woniuxy.java106crm.entity.User;
import com.woniuxy.java106crm.mapper.UserMapper;
import com.woniuxy.java106crm.request.Message;
import com.woniuxy.java106crm.request.ResPage;
import com.woniuxy.java106crm.request.ResponseResult;
import com.woniuxy.java106crm.request.StateCode;
import com.woniuxy.java106crm.service.MarketingService;
import com.woniuxy.java106crm.service.PlanService;
import com.woniuxy.java106crm.service.UserService;
import com.woniuxy.java106crm.vo.marketing.PlanVo;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Api(tags = "客户开发计划API文档")
@Slf4j
@RestController
@RequestMapping("/plan")
public class PlaController {
    @Resource
    private PlanService planService;

    @Resource
    private UserService userService;

    @Resource
    private MarketingService marketingService;

    /**
     * 在指派初始化计划
     */
    @RequireMethod("指派初始化计划")
    @PostMapping("/addStartPlan")
    public ResponseResult addStartPlan(@RequestBody PlanVo planVo){

        return new ResponseResult(StateCode.SUCCESS, Message.SUCCESS, planService.addStartPlan(planVo));
    }

    /**
     * 查询当前用户的计划
     */
    @RequireMethod("查询当前用户的计划")
    @GetMapping("/selectPlan")
    public ResponseResult selectPlan(HttpSession session){
        int uid = (int)session.getAttribute("uid");
        // 查询对应的用户名
        User user = userService.findById(uid);
        return new ResponseResult<>(StateCode.SUCCESS, Message.SUCCESS, planService.selectPlan(user.getUsername()));
    }

    /**
     * 查询计划的所有子计划
     */
    @RequireMethod("查询计划的所有子计划")
    @GetMapping("/selectAll/{page}/{size}/{lid}/{customerName}")
    public ResponseResult selectAll(@PathVariable("page") int page, @PathVariable("size") int size, @PathVariable("lid") int lid, @PathVariable("customerName") String customerName){
        PlanVo planVo = new PlanVo();
        planVo.setLid(lid);
        planVo.setCustomerName(customerName);
        ResPage<List<Plan>> listResPage = planService.selectAll(page,size,planVo);
        return new ResponseResult(StateCode.SUCCESS, Message.SUCCESS,listResPage );
    }

    /**
     * 根据id查询计划
     */
    @RequireMethod("根据id查询计划")
    @GetMapping("/findById/{id}")
    public ResponseResult findById(@PathVariable("id") int id){
        return new ResponseResult(StateCode.SUCCESS, Message.SUCCESS, planService.findById(id));
    }

    /**
     * 根据id修改计划
     */
    @RequireMethod("根据id修改计划")
    @PostMapping("/updateById")
    public ResponseResult updateById(@RequestBody Plan plan){
        System.out.println(plan);
        return new ResponseResult(StateCode.SUCCESS, Message.SUCCESS, planService.updateById(plan));
    }

    /**
     * 根据计划id查询机会和计划的关联数据
     */
    @RequireMethod("根据计划id查询机会和计划的关联数据")
    @GetMapping("/selectLP/{id}")
    public ResponseResult selectLP(@PathVariable("id") int id){
        return new ResponseResult(StateCode.SUCCESS, Message.SUCCESS, planService.selectLP(id));
    }

    /**
     * 增加按钮增加新的计划
     */
    @RequireMethod("增加按钮增加新的计划")
    @PostMapping("/addAllPlan")
    public ResponseResult addAllPlan(@RequestBody Plan plan, HttpSession session){
        String time = marketingService.selectByAppointTime(plan.getLid());
        // 获取时间并设置给新对象
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        // 解析字符串为LocalDateTime
        LocalDate date = LocalDate.parse(time, dateTimeFormatter);
        plan.setAppointTime(date);

        // 获取用户名字并设置给新对象
        System.out.println(plan);
        int uid = (int)session.getAttribute("uid");
        User user = userService.findById(uid);
        plan.setUname(user.getUsername());

        // 设置状态
        plan.setState(2);
        return new ResponseResult<>(StateCode.SUCCESS, Message.SUCCESS, planService.addAllPlan(plan));
    }

    /**
     * 根据id删除计划
     */
    @RequireMethod("根据id删除计划")
    @DeleteMapping("/delPlanById/{id}")
    public ResponseResult delPlanById(@PathVariable("id") int id){
        return new ResponseResult<>(StateCode.SUCCESS, Message.SUCCESS, planService.delPlanById(id));
    }

    /**
     * 根据id修改计划状态
     */
    @RequireMethod("根据id修改计划状态")
    @PostMapping("/updateState")
    public ResponseResult updateState(@RequestBody Plan plan){
        plan.setState(1);
        return new ResponseResult<>(StateCode.SUCCESS, Message.SUCCESS, planService.updateById(plan));
    }
}
